/*  mudmap - A tool to create and organize user side maps for MUD games
 *  Copyright (C) 2012  Neop (aka moemneop, email: mneop@web.de)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include "mainwindow.h"

namespace dia{
namespace mainwindow{

/// initializes the window
void init_gui();
/// initializes the overview tab
void init_overview();
/// creates the mudmap data directory, if it doesn't exist yet
void create_dir();

/// all mapdrawing tabs
vector<shared_ptr<mapdrawing> > mapds;
/// the current mapdrawing tab
shared_ptr<mapdrawing> cur_mapd;
/// the available worlds (used in the overview dialog)
vector<pair<string, string> > available_worlds;
bool mode_debug, mode_small_tiles, mode_aspect_ratio, mode_way_lines_show, mode_way_lines_curved;

config mudmap_config;

GtkWidget *window;
GtkWidget *notebook;

void about_show();
void manual_show();
void close();
void save();

// ---------------- funcs / button handlers -------------------
// these functions will be called by menu items
void menu_item_new_world_func(GtkMenuItem *menuitem, gpointer user_data);
void menu_item_open_world_func(GtkMenuItem *menuitem, gpointer user_data);
void menu_item_edit_world_func(GtkMenuItem *menuitem, gpointer user_data);
void menu_item_set_home_func(GtkMenuItem *menuitem, gpointer user_data);
void menu_item_new_area_func(GtkMenuItem *menuitem, gpointer user_data);
void menu_item_save_image_func(GtkMenuItem *menuitem, gpointer user_data);
void menu_item_debug_func(GtkMenuItem *menuitem, gpointer user_data);
void menu_item_way_lines_show_func(GtkMenuItem *menuitem, gpointer user_data);
void menu_item_way_lines_curved_func(GtkMenuItem *menuitem, gpointer user_data);

// these functions will be called by toolbar items
void toolbar_back_func(GtkToolButton *toolbutton, gpointer user_data);
void toolbar_forward_func(GtkToolButton *toolbutton, gpointer user_data);
void toolbar_home_func(GtkToolButton *toolbutton, gpointer user_data);
void toolbar_small_tiles_func(GtkToolButton *toolbutton, gpointer user_data);
void toolbar_aspect_ratio_func(GtkToolButton *toolbutton, gpointer user_data);
void toolbar_list_places_func(GtkMenuItem *menuitem, gpointer user_data);
void toolbar_search_func(GtkMenuItem *menuitem, gpointer user_data);

void overview_open_world_func(GtkToolButton *toolbutton, gpointer user_data);
gboolean switch_page_func(GtkNotebook *notebook, gpointer page, guint pagenum, gpointer user_data);
void remove_page_func(GtkNotebook *notebook, GtkWidget *child, guint page_num, gpointer user_data);

/// initialize the main window
void init(){
    mode_debug = mode_small_tiles = false;
    mudmap_config.read(path::get_worlds_dir() + path::global_config_file_str);
    #ifdef __arm__
    mode_way_lines_show = mudmap_config.get_bool("ways_lines_shown", false);
    mode_way_lines_curved = mudmap_config.get_bool("ways_lines_curved", false);
    #else
    mode_way_lines_show = mudmap_config.get_bool("ways_lines_shown", true);
    mode_way_lines_curved = mudmap_config.get_bool("ways_lines_curved", true);
    #endif // __arm__

    mode_small_tiles = mudmap_config.get_bool("small_tiles", false);
    mode_aspect_ratio = mudmap_config.get_bool("aspect_ratio", false);

    create_dir();
    mapdrawing::cur_mapd = &cur_mapd;
    mapdrawing::mode_debug = &mode_debug;
    mapdrawing::mode_small_tiles = &mode_small_tiles;
    mapdrawing::mode_aspect_ratio = &mode_aspect_ratio;
    mapdrawing::mode_way_lines_show = &mode_way_lines_show;
    mapdrawing::mode_way_lines_curved = &mode_way_lines_curved;
    init_gui();
    gtk_main();
    for(unsigned i = 0; i < mapds.size(); i++) if(mapds[i] != nullptr) mapdrawing::close_func(nullptr, &(*mapds[i]));
    worldmgr::get_instance()->write_available();
}

bool file_exist(string file){
    ifstream ifs(file);
    if(ifs.is_open()) return true;
    return false;
}

/// creates the mudmap data directory, if it doesn't exist yet
void create_dir(){
    if(file_exist("./worlds/worlds")) path::set_user_data_dir("./");
    else if(!file_exist(path::get_available_worlds_file())){
        GtkWidget *dialog = gtk_message_dialog_new(nullptr, (GtkDialogFlags) (GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL, _("MUD Map can be run in portable mode. A new directory \"worlds\" will be created in the directory of the executable. Otherwise the world files will be stored at a fixed location (\"~/.mudmap/worlds\" etc). Click OK to create a portable worlds directory or Cancel for a fixed directory."));
        gtk_widget_show_all(dialog);
        if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK){
            path::set_user_data_dir("./");
        }
        gtk_widget_destroy(dialog);

        ofstream ofs_test(path::get_available_worlds_file());
        if(!ofs_test.is_open()){
            #ifdef __linux__
                string sysstr = "mkdir -p " + path::get_worlds_dir();
            #else
                string sysstr = "mkdir \"" + path::get_worlds_dir() + "\"";
            #endif
            cout << "Directory \"" + path::get_worlds_dir() + " couldn't be accessed, executing " << sysstr << endl;
            if(system(sysstr.c_str()) != 0) cout << "Couldn't create directory " << path::get_worlds_dir() << ", worlds and other data might not be saved!";
        }
    }
}

/// initializes the window
void init_gui(){
    // create the window
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), properties::prog_name);
    g_signal_connect(window, "delete-event", G_CALLBACK(close), nullptr);

    // create widgets
    GtkWidget *box_v1 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_container_add(GTK_CONTAINER(window), box_v1);
    GtkWidget *menubar = gtk_menu_bar_new();
    gtk_box_pack_start(GTK_BOX(box_v1), menubar, false, true, 0);

    // create the menu items
    GtkWidget *menu_item_file = gtk_image_menu_item_new_from_stock(GTK_STOCK_FILE, nullptr);
    gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item_file), nullptr);
    gtk_menu_shell_append(GTK_MENU_SHELL(menubar), menu_item_file);
    GtkWidget *menu_item_edit = gtk_image_menu_item_new_from_stock(GTK_STOCK_EDIT, nullptr);
    gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item_edit), nullptr);
    gtk_menu_shell_append(GTK_MENU_SHELL(menubar), menu_item_edit);
    GtkWidget *menu_item_help = gtk_image_menu_item_new_from_stock(GTK_STOCK_HELP, nullptr);
    gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item_help), nullptr);
    gtk_menu_shell_append(GTK_MENU_SHELL(menubar), menu_item_help);

    GtkWidget *menu_file = gtk_menu_new();
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item_file), menu_file);
    GtkWidget *menu_edit = gtk_menu_new();
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item_edit), menu_edit);
    GtkWidget *menu_help = gtk_menu_new();
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item_help), menu_help);

    GtkWidget *menu_file_new_world = gtk_image_menu_item_new_from_stock(GTK_STOCK_NEW, nullptr);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu_file), menu_file_new_world);
    GtkWidget *menu_file_open_world = gtk_image_menu_item_new_from_stock(GTK_STOCK_OPEN, nullptr);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu_file), menu_file_open_world);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu_file), gtk_separator_menu_item_new());
    GtkWidget *menu_file_save_world = gtk_image_menu_item_new_from_stock(GTK_STOCK_SAVE, nullptr);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu_file), menu_file_save_world);
    GtkWidget *menu_file_save_image = gtk_image_menu_item_new_from_stock(GTK_STOCK_SAVE_AS, nullptr);
    gtk_menu_item_set_label(GTK_MENU_ITEM(menu_file_save_image), _("Save as image"));
    gtk_menu_shell_append(GTK_MENU_SHELL(menu_file), menu_file_save_image);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu_file), gtk_separator_menu_item_new());
    GtkWidget *menu_file_quit = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, nullptr);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu_file), menu_file_quit);

    GtkWidget *menu_edit_add_area = gtk_image_menu_item_new_from_stock(GTK_STOCK_ADD, nullptr);
    gtk_menu_item_set_label(GTK_MENU_ITEM(menu_edit_add_area), _("Add area"));
    gtk_menu_shell_append(GTK_MENU_SHELL(menu_edit), menu_edit_add_area);
    GtkWidget *menu_edit_set_home = gtk_image_menu_item_new_from_stock(GTK_STOCK_HOME, nullptr);
    gtk_menu_item_set_label(GTK_MENU_ITEM(menu_edit_set_home), _("Set home position"));
    gtk_menu_shell_append(GTK_MENU_SHELL(menu_edit), menu_edit_set_home);
    GtkWidget *menu_edit_world = gtk_image_menu_item_new_from_stock(GTK_STOCK_EDIT, nullptr);
    gtk_menu_item_set_label(GTK_MENU_ITEM(menu_edit_world), _("Edit world"));
    gtk_menu_shell_append(GTK_MENU_SHELL(menu_edit), menu_edit_world);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu_edit), gtk_separator_menu_item_new());
    GtkWidget *menu_edit_way_lines_show = gtk_check_menu_item_new_with_label(_("Show ways"));
    gtk_menu_shell_append(GTK_MENU_SHELL(menu_edit), menu_edit_way_lines_show);
    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_edit_way_lines_show), mode_way_lines_show);
    GtkWidget *menu_edit_way_lines_curved = gtk_check_menu_item_new_with_label(_("Curved ways"));
    gtk_menu_shell_append(GTK_MENU_SHELL(menu_edit), menu_edit_way_lines_curved);
    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_edit_way_lines_curved), mode_way_lines_curved);

    GtkWidget *menu_help_manual = gtk_image_menu_item_new_from_stock(GTK_STOCK_HELP, nullptr);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu_help), menu_help_manual);
    GtkWidget *menu_help_about = gtk_image_menu_item_new_from_stock(GTK_STOCK_ABOUT, nullptr);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu_help), menu_help_about);
    #ifdef DEBUGMODE
    gtk_menu_shell_append(GTK_MENU_SHELL(menu_help), gtk_separator_menu_item_new());
    GtkWidget *menu_help_debug = gtk_check_menu_item_new_with_label(_("Debug mode"));
    gtk_menu_shell_append(GTK_MENU_SHELL(menu_help), menu_help_debug);
    g_signal_connect(menu_help_debug, "activate", G_CALLBACK(menu_item_debug_func), nullptr);
    #endif

    g_signal_connect(menu_file_new_world, "activate", G_CALLBACK(menu_item_new_world_func), nullptr);
    g_signal_connect(menu_file_open_world, "activate", G_CALLBACK(menu_item_open_world_func), nullptr);
    g_signal_connect(menu_file_save_world, "activate", G_CALLBACK(save), nullptr);
    g_signal_connect(menu_file_save_image, "activate", G_CALLBACK(menu_item_save_image_func), nullptr);
    g_signal_connect(menu_file_quit, "activate", G_CALLBACK(close), nullptr);

    g_signal_connect(menu_edit_add_area, "activate", G_CALLBACK(menu_item_new_area_func), nullptr);
    g_signal_connect(menu_edit_set_home, "activate", G_CALLBACK(menu_item_set_home_func), nullptr);
    g_signal_connect(menu_edit_world, "activate", G_CALLBACK(menu_item_edit_world_func), nullptr);
    g_signal_connect(menu_edit_way_lines_show, "activate", G_CALLBACK(menu_item_way_lines_show_func), nullptr);
    g_signal_connect(menu_edit_way_lines_curved, "activate", G_CALLBACK(menu_item_way_lines_curved_func), nullptr);

    g_signal_connect(menu_help_about, "activate", G_CALLBACK(about_show), nullptr);
    g_signal_connect(menu_help_manual, "activate", G_CALLBACK(manual_show), nullptr);

    // create the toolbar
    GtkWidget *toolbar = gtk_toolbar_new();
    gtk_box_pack_start(GTK_BOX(box_v1), toolbar, false, true, 0);

    GtkToolItem *toolbar_previous = gtk_tool_button_new_from_stock(GTK_STOCK_GO_BACK);
    gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(toolbar_previous), _("go to the previous place"));
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar), toolbar_previous, -1);
    GtkToolItem *toolbar_forward = gtk_tool_button_new_from_stock(GTK_STOCK_GO_FORWARD);
    gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(toolbar_forward), _("go to the next place"));
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar), toolbar_forward, -1);
    GtkToolItem *toolbar_home = gtk_tool_button_new_from_stock(GTK_STOCK_HOME);
    gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(toolbar_home), _("go to the home place"));
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar), toolbar_home, -1);
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar), gtk_separator_tool_item_new(), -1);

    GtkToolItem *toolbar_save = gtk_tool_button_new_from_stock(GTK_STOCK_SAVE);
    gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(toolbar_save), _("save the current world"));
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar), toolbar_save, -1);
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar), gtk_separator_tool_item_new(), -1);

    //GtkToolItem *toolbar_small_tiles = gtk_toggle_tool_button_new_from_stock(GTK_STOCK_ZOOM_OUT);
    GtkToolItem *toolbar_small_tiles = gtk_tool_button_new_from_stock(GTK_STOCK_ZOOM_OUT);
    gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(toolbar_small_tiles), _("small / large tiles"));
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar), toolbar_small_tiles, -1);
    GtkToolItem *toolbar_aspect_ratio = gtk_toggle_tool_button_new_from_stock(GTK_STOCK_ORIENTATION_LANDSCAPE);
    gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(toolbar_aspect_ratio), _("change the aspect ratio of the tiles"));
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar), toolbar_aspect_ratio, -1);
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar), gtk_separator_tool_item_new(), -1);

    GtkToolItem *toolbar_list_places = gtk_tool_button_new_from_stock(GTK_STOCK_INDEX);
    gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(toolbar_list_places), _("list places"));
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar), toolbar_list_places, -1);
    GtkToolItem *toolbar_search = gtk_tool_button_new_from_stock(GTK_STOCK_FIND);
    gtk_tool_item_set_tooltip_text(GTK_TOOL_ITEM(toolbar_search), _("search for places and comments"));
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar), toolbar_search, -1);

    g_signal_connect(toolbar_previous, "clicked", G_CALLBACK(toolbar_back_func), nullptr);
    g_signal_connect(toolbar_forward, "clicked", G_CALLBACK(toolbar_forward_func), nullptr);
    g_signal_connect(toolbar_home, "clicked", G_CALLBACK(toolbar_home_func), nullptr);
    g_signal_connect(toolbar_save, "clicked", G_CALLBACK(save), nullptr);
    g_signal_connect(toolbar_small_tiles, "clicked", G_CALLBACK(toolbar_small_tiles_func), nullptr);
    g_signal_connect(toolbar_aspect_ratio, "clicked", G_CALLBACK(toolbar_aspect_ratio_func), nullptr);
    g_signal_connect(toolbar_list_places, "clicked", G_CALLBACK(toolbar_list_places_func), nullptr);
    g_signal_connect(toolbar_search, "clicked", G_CALLBACK(toolbar_search_func), nullptr);

    // create the notebook
    notebook = gtk_notebook_new();
    g_signal_connect(notebook, "switch-page", G_CALLBACK(switch_page_func), nullptr);
    g_signal_connect(notebook, "page-removed", G_CALLBACK(remove_page_func), nullptr);
    gtk_box_pack_start(GTK_BOX(box_v1), notebook, true, true, 0);

    worldmgr::get_instance()->update_dialog(overview_open_world_func);
    gtk_notebook_append_page(GTK_NOTEBOOK(notebook), worldmgr::get_instance()->get_content(), worldmgr::get_instance()->get_label());

    gtk_widget_show_all(window);
}

/// open a new world
void open_world(string file){
    // don't open an already loaded world again
    for(unsigned i = 0; i < mapds.size(); ++i) if(mapds[i]->get_world()->file == file){
        gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), gtk_notebook_page_num(GTK_NOTEBOOK(notebook), mapds[i]->get_content()));
        return;
    }

    shared_ptr<mapdrawing> tmp = shared_ptr<mapdrawing>(new mapdrawing(file, notebook, window));
    mapds.push_back(tmp);
    gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), gtk_notebook_append_page(GTK_NOTEBOOK(notebook), mapds.back()->get_content(), mapds.back()->get_label()));
    gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(notebook), mapds.back()->get_content(), true);
}

void about_show(){
    gchar *authors[2] = {(gchar*) properties::authors, nullptr};
    gtk_show_about_dialog(GTK_WINDOW(window), "authors", (const gchar**) authors,
                                              "program_name", properties::prog_name,
                                              "version", FULLVERSION_STRING,
                                              "website", properties::website,
                                              "license_type", GTK_LICENSE_GPL_3_0,
                                              "translator_credits", _("translator-credits"),
                                              "comments", properties::about_comments,
                                              nullptr);
}

void manual_show(){
    gtk_show_uri(nullptr, properties::user_manual_link, GDK_CURRENT_TIME, nullptr);
}

void close(){
    gtk_main_quit();
}

void save(){
    if(cur_mapd){
        cur_mapd->save_meta();
        cur_mapd->get_world()->save();
    }
}

// ---------------- funcs / button handlers -------------------
void menu_item_new_world_func(GtkMenuItem *menuitem, gpointer user_data){
    world_new(window, open_world);
    worldmgr::get_instance()->write_available();
}

void menu_item_open_world_func(GtkMenuItem *menuitem, gpointer user_data){
    string file = dia::world_file_chooser(window);
    if(file != "") open_world(file);
}

void menu_item_edit_world_func(GtkMenuItem *menuitem, gpointer user_data){
    if(cur_mapd){
        dia::world_edit(window, cur_mapd->get_world());
        cur_mapd->draw();
    }
}

void menu_item_set_home_func(GtkMenuItem *menuitem, gpointer user_data){
    if(cur_mapd) cur_mapd->set_home();
}

void menu_item_way_lines_show_func(GtkMenuItem *menuitem, gpointer user_data){
    mode_way_lines_show = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem));
    mudmap_config.set_bool("ways_lines_shown", mode_way_lines_show);
    mudmap_config.write();
    cur_mapd->draw();
}

void menu_item_way_lines_curved_func(GtkMenuItem *menuitem, gpointer user_data){
    mode_way_lines_curved = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem));
    mudmap_config.set_bool("ways_lines_curved", mode_way_lines_curved);
    mudmap_config.write();
    cur_mapd->draw();
}

void toolbar_list_places_func(GtkMenuItem *menuitem, gpointer user_data){
    if(cur_mapd){
        cur_mapd->place_call_list_add_cur_place();
        dia::list_places(window, cur_mapd->get_world(), &cur_mapd, goto_place);
    }
}

void toolbar_search_func(GtkMenuItem *menuitem, gpointer user_data){
    if(cur_mapd){
        cur_mapd->place_call_list_add_cur_place();
        dia::search(window, cur_mapd->get_world(), &cur_mapd, goto_place);
    }
}

void menu_item_new_area_func(GtkMenuItem *menuitem, gpointer user_data){
    if(cur_mapd) cur_mapd->mi_area_new_func(window, &(*cur_mapd));
}

void menu_item_save_image_func(GtkMenuItem *menuitem, gpointer user_data){
    if(cur_mapd){
        string file = dia::save_as_image(window);
        if(file[0] == 'c'){
            cur_mapd->save_screen_as_png(file.erase(0, 1));
            cur_mapd->draw();
        } else if(file[0] == 'e'){
            cur_mapd->save_layer_as_png(file.erase(0, 1), cur_mapd->cur_layer);
        }
    }
}

void menu_item_debug_func(GtkMenuItem *menuitem, gpointer user_data){
    mode_debug = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem));
    if(cur_mapd) cur_mapd->draw();
}

void toolbar_back_func(GtkToolButton *toolbutton, gpointer user_data){
    if(cur_mapd) cur_mapd->goto_previous();
}

void toolbar_forward_func(GtkToolButton *toolbutton, gpointer user_data){
    if(cur_mapd) cur_mapd->goto_next();
}

void toolbar_home_func(GtkToolButton *toolbutton, gpointer user_data){
    if(cur_mapd) cur_mapd->goto_home();
}

void toolbar_small_tiles_func(GtkToolButton *toolbutton, gpointer user_data){
    //mode_small_tiles = gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(toolbutton));
    if((mode_small_tiles = strcmp(gtk_tool_button_get_stock_id(toolbutton), GTK_STOCK_ZOOM_OUT) == 0))
        gtk_tool_button_set_stock_id(toolbutton, GTK_STOCK_ZOOM_IN);
    else gtk_tool_button_set_stock_id(toolbutton, GTK_STOCK_ZOOM_OUT);
    mudmap_config.set_bool("small_tiles", mode_small_tiles);
    mudmap_config.write();
    if(cur_mapd) cur_mapd->draw();
}

void toolbar_aspect_ratio_func(GtkToolButton *toolbutton, gpointer user_data){
    mode_aspect_ratio = gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(toolbutton));
    mudmap_config.set_bool("aspect_ratio", mode_aspect_ratio);
    mudmap_config.write();
    if(cur_mapd) cur_mapd->draw();
}

void overview_open_world_func(GtkToolButton *toolbutton, gpointer user_data){
    open_world(*((string*) user_data));
}

gboolean switch_page_func(GtkNotebook *notebook, gpointer page, guint pagenum, gpointer user_data){
    string titlestr;
    cur_mapd = nullptr;
    GtkWidget *label = gtk_notebook_get_tab_label(notebook, gtk_notebook_get_nth_page(notebook, pagenum));
    for(unsigned i = 0; i < mapds.size(); i++) if(mapds[i]->get_label() == label){
        titlestr = mapds[i]->get_world()->name;
        cur_mapd = mapds[i];
    }
    if(titlestr == "") gtk_window_set_title(GTK_WINDOW(window), properties::prog_name);
    else gtk_window_set_title(GTK_WINDOW(window), (titlestr + " - " + properties::prog_name).c_str());
    return true;
}

void remove_page_func(GtkNotebook *notebook, GtkWidget *child, guint page_num, gpointer user_data){
    int remove_num = 0;
    for(unsigned i = 0;i < mapds.size(); i++){
        if(mapds[i] != nullptr && mapds[i]->get_content() != child && !mapds[i]->close){
            // if at least one element was removed
            if(remove_num) mapds[i - remove_num] = mapds[i];
        } else ++remove_num;
    }
    for(int i = 0; i < remove_num; ++i) mapds.pop_back();
}

}
}
